// License: http://www.codeplex.com/entlibcontrib/Project/License.aspx
// Author: Randy

using System;
using System.Diagnostics;
using System.IO;

using Microsoft.Practices.EnterpriseLibrary.Logging;

using EntLibContrib.Logging.Parser;
using EntLibContrib.Logging.Formatters;

#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif

namespace EntLibContrib.Logging.UnitTests.Parser
{
    [TestClass]
    public class TimeStampFunctionLogFileReaderFromMemory
    {
        [TestMethod]
        public void TestReaderFromMemory()
        {
            const string templateData = "{timestamp(o)}{tab}{category}{tab}{severity}{tab}{priority}{tab}{eventid}{tab}{machine}{tab}{processName}{tab}{message}{tab}{title}";
            StringWriter myStringWriter = new StringWriter();
            TemplateTextFormatter myTemplateTextFormatter = new TemplateTextFormatter(templateData);
            LogEntry writeLogEntry = new LogEntry("My Message", "Any category", 123, 456,
                TraceEventType.Verbose, "Additional Information", null);
            myStringWriter.WriteLine(myTemplateTextFormatter.Format(writeLogEntry));
            TimeStampFunctionLogFileReader myReader = new TimeStampFunctionLogFileReader(
                null, myTemplateTextFormatter, null);
            string logFileText = myStringWriter.GetStringBuilder().ToString();
            int entryCount = 0;
            foreach (LogEntry readLogEntry in myReader.ParseLogText(logFileText, null))
            {
                Assert.AreEqual(readLogEntry.TimeStamp.Hour, writeLogEntry.TimeStamp.Hour);
                Assert.AreEqual(readLogEntry.Message, writeLogEntry.Message);
                Assert.AreEqual(readLogEntry.Priority, writeLogEntry.Priority);
                Assert.AreEqual(readLogEntry.EventId, writeLogEntry.EventId);
                Assert.AreEqual(readLogEntry.Severity, writeLogEntry.Severity);
                Assert.AreEqual(readLogEntry.Title, writeLogEntry.Title);
                entryCount++;
            }
            if (entryCount != 1)
                Assert.Fail("Incorrect number of log entries");
        }
    }
}
